<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category ExceptionErrorController.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/3/22  16:35
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin\V1;


use App\Services\ExceptionErrorService;
use App\Util\FileSystem;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Illuminate\Http\Request;

class ExceptionErrorController extends Controller
{
    protected $exceptionErrorService;

    public function __construct(ExceptionErrorService $exceptionErrorService)
    {
        $this->exceptionErrorService = $exceptionErrorService;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @Depiction 获取列表
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/23  19:48
     */
    public function index(Request $request)
    {
        $rules    = [
            'id'       => ['nullable', 'string',],
            'message'  => ['nullable', 'string',],
            'is_solve' => ['nullable', 'string',],
        ];
        $messages = [

        ];
        $this->validate($request, $rules, $messages);

        $params = $request->all();

        $data = $this->exceptionErrorService->getList($params);

        return $this->responseSuccessData('请求成功！', $data);

    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction 修复信息
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/23  20:23
     */
    public function amended(Request $request)
    {
        $rules      = [
            'id'    => ['required', 'string', 'exists:exception_errors'],
            'solve' => ['required', 'integer'],
        ];
        $attributes = [
            'id'    => '错误ID',
            'solve' => '修复值',
        ];
        $messages   = [];
        $this->validate($request, $rules, $messages, $attributes);

        $exception = $this->exceptionErrorService->getInfo($request->input('id'));
        if (empty($exception)) {
            return $this->responseErrorData('数据异常！');
        }

        $result = $this->exceptionErrorService->solve($exception);

        if ($result == false) {
            return $this->responseErrorData('修复失败！');
        } else {
            return $this->responseSuccessData('修复成功！');
        }
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * @Depiction 获取文件列表
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/24  20:39
     */
    public function files()
    {
        $fileSystem = new FileSystem('/', 'logs');

        $data = $fileSystem->files();

        return $this->responseSuccessData('获取成功！', $data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\ValidationException
     * @Depiction 获取文件信息
     * @Author hwy <429208948@qq.com>
     * @Date 2022/3/24  21:19
     */
    public function file(Request $request)
    {
        $rules      = [
            'file' => ['required', 'string'],
        ];
        $attributes = [
            'file' => '文件',
        ];
        $messages   = [];
        $this->validate($request, $rules, $messages, $attributes);

        try {
            $fileSystem = new FileSystem('/', 'logs');

            $data = $fileSystem->getDisk()->get($request['file']);

            return $this->responseSuccessData('获取成功！', $data);

        } catch (FileNotFoundException $exception) {
            return $this->responseErrorData($exception->getPrevious()->getMessage());
        }


    }


}
